﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;

public partial class UserLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOK_Click(object sender, EventArgs e)
    {

        UserInfo info = Yipusoft.Account.BLL.User.GetOAUserInfo(UserId.Text, Password.Text);
        if (info.UserID != null)//登录OA成功
        {
            if (Yipusoft.Account.BLL.User.ValidateLogin(UserId.Text, Password.Text) == 1)//登录FireMis
            {
                //部门
                info.DepartmentID = "13" + info.DepartmentID;
                string OAdepartmentId = info.DepartmentID;
                if (OAdepartmentId == "1300048")
                {
                    info.DepartmentID = "1300011";//指挥中心
                }
                if (OAdepartmentId == "1312000")
                {
                    info.DepartmentID = "1312100";//教导大队
                }
                if (OAdepartmentId == "1312001")
                {
                    info.DepartmentID = "1312101";//办公室
                }
                if (OAdepartmentId == "1312002")
                {
                    info.DepartmentID = "1312102";//教研室
                }
                if (OAdepartmentId == "1312003")
                {
                    info.DepartmentID = "1312103";//汽训队
                }
                if (OAdepartmentId == "1313000")
                {
                    info.DepartmentID = "1313100";//北戴河
                }
                //石家庄
                if (OAdepartmentId == "1301314")
                {
                    info.DepartmentID = "1301307";//特勤-中队
                }
                else
                    if (OAdepartmentId == "1301307")
                    {
                        info.DepartmentID = "1301314";//七中队
                    }
                if (OAdepartmentId == "1301315")
                {
                    info.DepartmentID = "1301308";//特勤二中队
                }
                else
                    if (OAdepartmentId == "1301308")
                    {
                        info.DepartmentID = "1301315";//八中队
                    }

                if (OAdepartmentId == "1302365")
                {
                    info.DepartmentID = "1302369";//迁安二中队
                }
                if (OAdepartmentId == "1302356")
                {
                    info.DepartmentID = "1302367";//遵化中队
                }
                if (OAdepartmentId == "1302354")
                {
                    info.DepartmentID = "1302368";//迁安一中队
                }
                else if (OAdepartmentId == "1302367")
                {
                    info.DepartmentID = "1302370";
                }
                else if (OAdepartmentId == "1302369")
                {
                    info.DepartmentID = "1302371";
                }
                else
                    if (OAdepartmentId == "1309355")//秦皇岛北戴河特勤中队
                    {
                        info.DepartmentID = "1309303";
                    }
                    else if (OAdepartmentId == "1300045")
                    {
                        info.DepartmentID = "1300050";
                    }
                    else if (OAdepartmentId == "1301043")
                    {
                        info.DepartmentID = "1301050";
                    }
                    else if (OAdepartmentId == "1302043")
                    {
                        info.DepartmentID = "1302050";
                    }
                    else if (OAdepartmentId == "1308043")
                    {
                        info.DepartmentID = "1308050";
                    }
                    else if (OAdepartmentId == "1314000")
                    {
                        info.DepartmentID = "1314100";
                    }


                DepartmentInfo OAdepartinfo = null;
                DepartmentInfo departinfo = Department.GetDepartment(info.DepartmentID);
                if (departinfo == null)//没有这个部门添加这个部门
                {
                    OAdepartinfo = Department.GetOADepartment(info.DepartmentID);
                    Department.Insert(OAdepartinfo);
                }
                else
                {
                    //用户的部门ID还为原来的部门ID
                    Yipusoft.Account.Model.UserInfo YWInfo = Yipusoft.Account.BLL.User.GetUser(info.UserID);
                    if (YWInfo.DepartmentID != info.DepartmentID)
                    {
                        YWInfo.DepartmentID = info.DepartmentID;
                        Yipusoft.Account.BLL.User.Update(YWInfo);
                    }
                    //OA里名称更改业务系统里也改变
                    //OAdepartinfo = Department.GetOADepartment(info.DepartmentID);
                    //departinfo = Department.GetDepartment(info.DepartmentID);
                    //if (departinfo.FullName != OAdepartinfo.FullName)
                    //{
                    //    departinfo.FullName = OAdepartinfo.FullName;
                    //    departinfo.ShortName = OAdepartinfo.ShortName;
                    //    Department.Update(departinfo);
                    //}
                }
                //用户是否有角色没有角色添加角色

                SetUserRole(info);
                Logins();
            }
            else//没有登录FireMis
            {

                UserInfo infof = Yipusoft.Account.BLL.User.GetUser(info.UserID);
                if (infof != null)//用户存在密码修改
                {
                    infof.Password = Password.Text;
                    Yipusoft.Account.BLL.User.Update(infof);
                    Logins();
                }
                else
                {
                    //部门
                    info.DepartmentID = "13" + info.DepartmentID;

                    string OAdepartmentId = info.DepartmentID;
                    if (OAdepartmentId == "1300048")
                    {
                        info.DepartmentID = "1300011";//指挥中心
                    }
                    if (OAdepartmentId == "1312000")
                    {
                        info.DepartmentID = "1312100";//教导大队
                    }
                    if (OAdepartmentId == "1312001")
                    {
                        info.DepartmentID = "1312101";//办公室
                    }
                    if (OAdepartmentId == "1312002")
                    {
                        info.DepartmentID = "1312102";//教研室
                    }
                    if (OAdepartmentId == "1312003")
                    {
                        info.DepartmentID = "1312103";//汽训队
                    }
                    if (OAdepartmentId == "1313000")
                    {
                        info.DepartmentID = "1313100";//北戴河
                    }
                    //石家庄
                    if (OAdepartmentId == "1301314")
                    {
                        info.DepartmentID = "1301307";//特勤-中队
                    }
                    else
                        if (OAdepartmentId == "1301307")
                        {
                            info.DepartmentID = "1301314";//七中队
                        }
                    if (OAdepartmentId == "1301315")
                    {
                        info.DepartmentID = "1301308";//特勤二中队
                    }
                    else
                        if (OAdepartmentId == "1301308")
                        {
                            info.DepartmentID = "1301315";//八中队
                        }

                    if (OAdepartmentId == "1302365")
                    {
                        info.DepartmentID = "1302369";//迁安二中队
                    }
                    if (OAdepartmentId == "1302356")
                    {
                        info.DepartmentID = "1302367";//遵化中队
                    }
                    if (OAdepartmentId == "1302354")
                    {
                        info.DepartmentID = "1302368";//迁安一中队
                    }
                    else
                        if (OAdepartmentId == "1309355")//秦皇岛北戴河特勤中队
                        {
                            info.DepartmentID = "1309303";
                        }
                        else if (OAdepartmentId == "1302367")
                        {
                            info.DepartmentID = "1302370";
                        }
                        else if (OAdepartmentId == "1302369")
                        {
                            info.DepartmentID = "1302371";
                        }
                        else if (OAdepartmentId == "1314000")
                        {
                            info.DepartmentID = "1314100";
                        }
                    DepartmentInfo departinfo = Department.GetDepartment(info.DepartmentID);
                    if (departinfo == null)//没有这个部门添加这个部门
                    {
                        DepartmentInfo OAdepartinfo = Department.GetOADepartment(info.DepartmentID);
                        Department.Insert(OAdepartinfo);
                    }
                    //添加用户
                    info.TimeLastLogin = DateTime.Now;
                    info.TimeRegister = DateTime.Now;
                    info.IPLastLogin = "";
                    info.State = 0;
                    Yipusoft.Account.BLL.User.Insert(info);

                    SetUserRole(info);

                    List<RoleInfo> Rolelist = Yipusoft.Account.BLL.User.GetRoleList(info.UserID);
                    if (Rolelist.Count == 0)
                    {
                        SetUserRole(info);
                    }
                    Logins();
                }
            }
        }
        else
        {

            if (Yipusoft.Account.BLL.User.ValidateLogin(UserId.Text, Password.Text) == 1)
            {
                UserInfo user = Yipusoft.Account.BLL.User.GetUser(UserId.Text);
                if (user.DepartmentID.Substring(5, 1) == "5"|| user.DepartmentID.Substring(0, 5) == "13142")
                {
			SetUserRole(user);
                    Logins();
                }
                else
                {
                    Prompt.Text = "错误的用户名或者密码！";
                }
            }
            else
            {
                Prompt.Text = "错误的用户名或者密码！";
            }
        }
    }

    private static void SetUserRole(UserInfo info)
    {
        //如果是总队的后勤或其它到部门到各部门
        if (info.DepartmentID.Substring(0, 6) == "130001")
        {
            Yipusoft.Account.BLL.User.AddRole(info.UserID, "100109");
        }
        else
            //是否是总队政治部的账号
            if (info.DepartmentID.Substring(0, 6) == "130002")
            {
                Yipusoft.Account.BLL.User.AddRole(info.UserID, "100106");
            }//后勤
            else if (info.DepartmentID.Substring(0, 6) == "130003")
            {
                Yipusoft.Account.BLL.User.AddRole(info.UserID, "100107");
            }
            //防火
            else if (info.DepartmentID.Substring(0, 6) == "130004")
            {
                Yipusoft.Account.BLL.User.AddRole(info.UserID, "100108");
            }
            //宣传中心
            else if (info.DepartmentID.Substring(0, 6) == "130005")
            {
                Yipusoft.Account.BLL.User.AddRole(info.UserID, "100112");
            }
            else//支队
            {
                //司令部
                if (info.DepartmentID.Substring(4, 1) == "0" && info.DepartmentID.Substring(5, 1) == "1")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100209");
                }
                //支队政治处
                else if (info.DepartmentID.Substring(4, 1) == "0" && info.DepartmentID.Substring(5, 1) == "2")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100206");
                }
                //支队后勤处
                else if (info.DepartmentID.Substring(4, 1) == "0" && info.DepartmentID.Substring(5, 1) == "3")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100207");
                }
                //支队防火处
                else if (info.DepartmentID.Substring(4, 1) == "0" && info.DepartmentID.Substring(5, 1) == "4")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100208");
                }
                else if (info.DepartmentID.Substring(4, 1) == "0" && info.DepartmentID.Substring(5, 1) == "5")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100113");
                }
                else if (info.DepartmentID.Substring(4, 1) == "2")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100220");
                }
                else if (info.DepartmentID.Substring(4, 1) == "3")
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "100210");
                }
                else
                {
                    Yipusoft.Account.BLL.User.AddRole(info.UserID, "999999");
                }
            }
    }
    private void Logins()
    {
        Session["DepartmentID"] = Yipusoft.Account.BLL.User.GetUser(UserId.Text).DepartmentID;
        FormsAuthentication.SetAuthCookie(UserId.Text, false);
        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 0;
        log.EventData = "";
        log.ItemID = "";
        log.ItemName = "";
        log.ItemFullUrl = "UserLogin.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);
        Response.Redirect("Default.aspx");
    }
    protected void btncancel_Click(object sender, EventArgs e)
    {
        UserId.Text = "";
        Password.Text = "";

    }
}
